package com.rjkj.medicalwaste.viewmodels

import androidx.lifecycle.LiveData
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.rjkj.medicalwaste.base.BaseViewModel
import com.rjkj.medicalwaste.bean.GarbageBean
import com.rjkj.medicalwaste.bean.GarbageStatus
import com.rjkj.medicalwaste.database.GarbageSetBoxed
import com.rjkj.medicalwaste.database.GarbageSetUnReview
import com.rjkj.medicalwaste.network.response.deal
import com.rjkj.medicalwaste.network.response.next
import com.rjkj.medicalwaste.repository.GarbageRepository
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.withContext

class UnBoxedViewModel(val repository: GarbageRepository) : BaseViewModel() {
    var firstLoad :Boolean = false
    var time:String = ""
    //复核医废袋更新数据
    suspend fun updateGarbageSetUnReview(garbageSetUnReview: GarbageSetUnReview): Int {
        return withContext(IO){
            repository.updateGarbageSetUnReview(garbageSetUnReview)
        }
    }

    //装箱医废袋更新数据
    suspend fun updateGarbageSetBoxed(vararg garbageSetBoxed: GarbageSetBoxed): Int {
        return withContext(IO){
            repository.updateGarbageSetBoxed(*garbageSetBoxed)
        }
    }

    fun getStateGarbageBeanList(hospitalId: String, state: String): LiveData<List<GarbageBean>> {
        return repository.getStateGarbageBeanList(hospitalId, state)
    }

    fun getStateGarbageByStoreCodeToList(hospitalId: String, state: String, reviewStorageQRCode: String): LiveData<List<GarbageBean>> {
        return repository.getStateGarbageByStoreCodeToList(hospitalId, state, reviewStorageQRCode)
    }

    fun getMultiStateGarbageBeanList(hospitalId: String, states: Array<String>): LiveData<List<GarbageBean>> {
        return repository.getMultiStateGarbageBeanList(hospitalId, states)
    }

    suspend fun getMultiStateGarbageBeanList(hospitalId: String, roomId: String, states: Array<String>): List<GarbageBean> {
        return repository.getMultiStateGarbageBeanList(hospitalId, roomId, states)
    }


    private fun getTempBoxCode(roomId: String, type: Int): String {
        return "A$type$roomId"
    }


    /**
     * 开箱弹窗的页数据
     * @param boxCode String
     * @return Flow<PagingData<GarbageBean>>
     */
    fun getOpenBoxGarbagePaging(boxCode: String): Flow<PagingData<GarbageBean>> {
        return Pager(config = PagingConfig(
                pageSize = 7,
                initialLoadSize = 7
        ), pagingSourceFactory = {
            repository.getOpenBoxGarbagePagingSource(boxCode)
        }).flow
    }
    suspend fun getReviewNotBoxGarbageByCleaner(
        url:String,
        cleanerQRCode: String = "",
        hospitalId: String,
        reviewStorageQRCode: String,
        serialNum :String
    ):List<GarbageBean>? {
        return request {
            getReviewNotBoxGarbageByCleaner(
                url = url,
                deviceSN = serialNum,
                cleanerQRCode = cleanerQRCode,
                hospitalId = hospitalId,
                reviewStorageQRCode = reviewStorageQRCode
            )
        }.deal()
    }
//    fun outboundBoxWithGarbage(boxWithGarbageList: List<BoxWithGarbage>) {
//        val list = mutableListOf<GarbageBean>()
//        boxWithGarbageList.forEach {
//            list.addAll(it.garbageList)
//        }
//        val bargeDealList = list.map {
//            GarbageRequestOutbound(
//                    id = it.id,
//                    garbageBoxQRCode = it.garbageBoxQRCode
//            )
//        }.toList()
//        val boxDealList = boxWithGarbageList.map {
//            BoxDao.BoxRequestStatus(
//                    id = it.boxBean.id,
//                    garbageBoxQRCode = it.boxBean.garbageBoxQRCode
//            )
//        }.toList()
////        viewModelScope.launch(Dispatchers.IO){
////            boxRepository.updateBoxOutbound(boxDealList)
////            repository.updateGarbageBeanState(bargeDealList)
////        }
//    }

//    /**
//     * 创建临时箱
//     * @param roomId UserBean
//     * @param cleaner UserBean
//     */
//    suspend fun createBox(roomId:UserBean,cleaner:UserBean){
//        MedicalWasteType.values().forEach {
//            if(it!=MedicalWasteType.NUllType){
//                val boxBean = TempBoxBean()
//                boxBean.cleanerName = cleaner.qrcode
//                boxBean.cleanerQRCode = cleaner.qrcode
//                boxBean.stagingRoomName = roomId.name
//                boxBean.stagingRoomQRCode = roomId.qrcode
//                boxBean.garbageBoxType = it.typeName
//                boxBean.tempBoxQRCode =getTempBoxCode(roomId.qrcode,it.num)
//                boxRepository.insertTempBoxBeanData(boxBean)
//            }
//        }
//
//    }

//    /**
//     * 更新医废的箱码 箱码是临时箱码每种类型只有一个
//     * @param garbageBean GarbageBean
//     * @param value UserBean? 暂存间二维码
//     */
//    fun updateGarbageToTempBox(garbageBean: GarbageBean, value: UserBean?,checkBox: Boolean = false,reviewWeight: String = ""){
//        viewModelScope.launch(Dispatchers.IO){
//            if (value != null) {
//                //找到临时箱码
//                val tempBox = boxRepository.findBoxByTempCode(getTempBoxCode(value.qrcode,
//                        MedicalWasteType.getTypeByTypeName(garbageBean.garbageType).num)
//                )
//                //将bag绑定到临时箱码上 并 修改状态
////                garbageBean.tempBoxQRCode = tempBox.tempBoxQRCode
//                val garbageRequestCode = GarbageRequestTempCode(
//                        id = garbageBean.id,
//                        tempBoxQRCode = garbageBean.tempBoxQRCode,
//                        garbageStatus = garbageBean.garbageStatus,
//                        reviewWeightState = garbageBean.reviewWeightState,
//                        reviewWeight = reviewWeight
//                )
//
//                //临时箱bag数量+1
//                if (repository.updateGarbageBeanTempCode(garbageRequestCode)>0){
//                    val tempBoxRequestCount = BoxDao.TempBoxRequestCount(
//                            tempBox.id,
//                            tempBox.tempBoxQRCode,
//                            ++tempBox.garbageCNT,
//                    )
//                    boxRepository.updateTempBoxCount(tempBoxRequestCount)
//                }
//
//                if(checkBox){
//                    val garbageBox = boxRepository.findBoxByBoxCode(garbageBean.garbageBoxQRCode)
//                    val boxRequestCount = BoxDao.BoxRequestCount(
//                            garbageBox.id,
//                            garbageBox.garbageBoxQRCode,
//                            --garbageBox.garbageCNT,
//                    )
//                    boxRepository.updateBoxCount(boxRequestCount)
//                }
//            }
//        }
//    }
//    fun updateGarbageToBox(garbageBean: GarbageBean,
//                           boxCode: String,
//                           getWeight:String,
//                           weightCheck:Boolean,
//                           value: UserBean?){
//        viewModelScope.launch(Dispatchers.IO){
//            if (value != null) {
//                val box = boxRepository.findBoxByBoxCode(boxCode)
//                val garbageRequestCode = GarbageRequestOpenBoxAddGarbage(
//                        id = garbageBean.id,
//                        garbageStatus = GarbageStatus.boxed,
//                        garbageWeight = getWeight,
//                        reviewWeightState = if(weightCheck){ReviewWeightState.CheckSuccess}else{ReviewWeightState.CheckFail},
//                        reviewTime = TimeUtils.getYMDTime(),
//                        garbageBoxQRCode = box.garbageBoxQRCode
//                )
//                repository.updateOpenBoxAddGarbage(garbageRequestCode)
//                //临时箱bag数量+1
//                val boxRequestCount = BoxDao.BoxRequestCount(
//                        box.id,
//                        box.garbageBoxQRCode,
//                        ++box.garbageCNT,
//                )
//                boxRepository.updateBoxCount(boxRequestCount)
////                if (repository.updateGarbageBeanTempCode(garbageRequestCode)>0){
//
////                    )
//
////                }
//
////                if(checkBox){
////                    val garbageBox = boxRepository.findBoxByBoxCode(garbageBean.garbageBoxQRCode)
////                    val boxRequestCount = BoxDao.BoxRequestCount(
////                            garbageBox.id,
////                            garbageBox.garbageBoxQRCode,
////                            --garbageBox.garbageCNT,
////                    )
////                    boxRepository.updateBoxCount(boxRequestCount)
////                }
//            }
//        }
//    }

//    /**
//     * 将临时箱封箱删除数据，，修改状态，赋予正式的箱码
//     */
//    fun sealingBox(tempBoxWithGarbage: TempBoxWithGarbage){
//        viewModelScope.launch(Dispatchers.IO){
//            val tempBoxBean = tempBoxWithGarbage.tempBoxBean
//            val boxBean = BoxBean()
//            boxBean.garbageBoxQRCode = StrUtils.genRandomNum(15)
//            boxBean.stagingRoomQRCode = tempBoxBean.stagingRoomQRCode
//            boxBean.stagingRoomName = tempBoxBean.stagingRoomName
//            boxBean.garbageBoxType = tempBoxBean.garbageBoxType
//            boxBean.cleanerQRCode = tempBoxBean.cleanerQRCode
//            boxBean.cleanerName = tempBoxBean.cleanerName
//            boxBean.garbageCNT = tempBoxBean.garbageCNT
//            val changeCode= boxBean.garbageBoxQRCode
//            boxRepository.insertBoxBeanData(boxBean)
//            boxRepository.updateTempBoxCount(BoxDao.TempBoxRequestCount(
//                    id = tempBoxBean.id,
//                    tempBoxQRCode = tempBoxBean.tempBoxQRCode,
//                    garbageCNT = 0)
//            )
//            repository.updateGarbageBeanCodeList(tempBoxWithGarbage.garbageList.map {
//                GarbageRequestCodeAndState(it.id,changeCode,GarbageStatus.boxed)
//            }.toList())
//
//
//        }
//    }


//    fun getAllBoxPaging(roomId: String ):Flow<PagingData<BoxBean>>{
//        return Pager(config = PagingConfig(
//                pageSize = 6,
//                enablePlaceholders = false,
//                initialLoadSize = 6
//        ), pagingSourceFactory = {
//            boxRepository.findAllBoxByRoomId(roomId)
//        }).flow
//    }


//    fun getBoxWithGarbageList(roomId: String,status: String): Flow<PagingData<BoxWithGarbage>>{
//        return Pager(config = PagingConfig(
//                pageSize = 7,
//                initialLoadSize = 7
//        ), pagingSourceFactory = {
//            boxRepository.getBoxWithGarbageList(roomId,status)
//        }).flow
//    }
//    fun getTempBoxWithGarbageList(roomId: String): Flow<PagingData<TempBoxWithGarbage>>{
//        return Pager(config = PagingConfig(
//                pageSize = 7,
//                enablePlaceholders = true,
//                initialLoadSize = 7
//        ), pagingSourceFactory = {
//            boxRepository.getTempBoxWithGarbageList(roomId)
//        }).flow
//    }


//
//    fun getMultiStateGarbageBeanPaging(hospitalId: String,states:Array<String> ):Flow<PagingData<GarbageBean>>{
//        return Pager(config = PagingConfig(
//                pageSize = 6,
//                enablePlaceholders = false,
//                initialLoadSize = 6
//        ), pagingSourceFactory = {
//            repository.getMultiStateGarbageBeanPagingSource(hospitalId, states)
//        }).flow
//    }

}